﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShareB.Models;

namespace ShareB.Controllers
{
    public class ConfigurationController : Controller
    {
        //inicia os serviços necessários
        FieldsService field_serv = new FieldsService();
        PubConfService conf_serv = new PubConfService();

        // Acção que mapeia o acesso genérico á manutenção geral do sistema ( nao precisa de um modelo)
        // GET: /Configuration/

        public ActionResult Index()
        {
            return View();
        }

        // Gerir Campos disponiveis
        // GET: /Configuration/ManageFields

        public ActionResult ManageFields()
        {          
            //Chama view com lista de Metafields
            return View(field_serv.GetAllMetaFields());
        }

        //
        // Get: /Configuration/CreateMeta

        public ActionResult CreateMeta()
        {
            return View();
        }

        //
        // Post: /Configuration/CreateMeta

        [HttpPost]
        public ActionResult CreateMeta(MetaFieldModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    field_serv.CreateMeta(model);
                    return RedirectToAction("ManageFields");
                }
                else
                    return View(model);
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", "An unexpected Error has ocurred: " + e.ToString());
                return View(model);
            }
        }

        //
        // GET: /Configuration/Edit/5

        public ActionResult EditMeta(int id)
        {
            return View(field_serv.GetMeta(id));
        }

        //
        // POST: /Configuration/Edit/5

        [HttpPost]
        public ActionResult EditMeta(MetaFieldModel model)
        {
            
            try
            {
                if (ModelState.IsValid)
                {
                    field_serv.UpdateMeta(model);
                    return RedirectToAction("ManageFields");
                }
                else return View(model);         
            }
            catch(Exception e)
            {
                ModelState.AddModelError("", "An unexpected Error has ocurred: " + e.ToString());
                return View(model);
            }
        }

        // Gerir as publicaçoes pré-definidas
        // GET: /Configuration/ManagePublications

        public ActionResult ManagePublications()
        {
            return View(conf_serv.GetAllConfigurations());
        }


        // Gerir as publicaçoes pré-definidas
        // GET: /Configuration/CreatePubConf

        public ActionResult CreatePubConf()
        {
            //instancia modelo para auxiliar a configuração
            NewPubConfModel model = new NewPubConfModel();
            model.includedFields = field_serv.GetAllFieldsToInclude();

            //passa modelo auxiliar para a view
            return View(model);
        }

        [HttpPost]
        public ActionResult CreatePubConf(NewPubConfModel mod)
        {
            PubConfModel model = (PubConfModel)mod;

            try
            {
                if (ModelState.IsValid)
                {                    
                    conf_serv.CreateConf(model);  //TODO
                    return RedirectToAction("ManagePublications");
                }
                else return View(model);
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", "An unexpected Error has ocurred: " + e.ToString());
                return View(model);
            }
        }

        // IGNORARAR TUDO O QUE TIVER ABAIXO DESTA MENSAGEM ---------------------------------------------------------------------------


        //
        // GET: /Configuration/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Configuration/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
